package com.ktdaddy.service.impl;

import com.ktdaddy.enums.WhaleErrorCode;
import com.ktdaddy.exception.BusinessRuntimeException;
import com.ktdaddy.manager.UserManager;
import com.ktdaddy.pojo.Kernel;
import com.ktdaddy.request.LoginReq;
import com.ktdaddy.enums.LoginCode;
import com.ktdaddy.request.PwdChangeReq;
import com.ktdaddy.service.PassportService;
import com.ktdaddy.response.LoginResultResp;
import com.ktdaddy.utils.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * @author kdaddy@163.com
 * @date 2021/4/29 22:28
 */
@Service
public class PassportServiceImpl implements PassportService {

    @Autowired
    private UserManager userManager;

    @Override
    public LoginResultResp login(LoginReq loginBO) {
        // 获取当前用户主体
        Subject subject = SecurityUtils.getSubject();
        LoginResultResp loginResultVO  = new LoginResultResp(true,LoginCode.LOGIN_SUCCESS);
        // 将用户名和密码封装成 UsernamePasswordToken 对象
        UsernamePasswordToken token = new UsernamePasswordToken(loginBO.getKernelName(), loginBO.getPassword(), loginBO.isRememberMe());
        try {
            subject.login(token);
        } catch (UnknownAccountException uae) { // 账号不存在
            loginResultVO.setLoginCode(LoginCode.UNKNOWN_ACCOUNT_ERROR);
            loginResultVO.setResult(false);

        } catch (IncorrectCredentialsException ice) { // 账号与密码不匹配

            loginResultVO.setLoginCode(LoginCode.INCORRECT_CREDENTIALS_ERROR);
            loginResultVO.setResult(false);

        } catch (LockedAccountException lae) { // 账号已被锁定

            loginResultVO.setLoginCode(LoginCode.LOCKED_ACCOUNT_ERROR);
            loginResultVO.setResult(false);

        } catch (AuthenticationException ae) { // 其他身份验证异常

            loginResultVO.setLoginCode(LoginCode.OTHER_ACCOUNT_ERROR);
            loginResultVO.setResult(false);
        }

        return loginResultVO;
    }

    @Override
    public void logout() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            subject.logout();
        }
    }

    @Override
    public void changePwd(PwdChangeReq pwdChangeReq) {
        // 校验旧密码是否正确
        Kernel kernel = userManager.getKernelByUserId(pwdChangeReq.getUserId());
        if(Objects.isNull(kernel)){
            throw new BusinessRuntimeException(WhaleErrorCode.GLOBAL_SYSTEM_ERROR);
        }
        String oldPwd = kernel.getPassword();
        String paramPwd = ShiroUtils.getEncryptPassword(kernel.getSalt(),pwdChangeReq.getPasswordOld(),kernel.getKernelName());
        if(!paramPwd.equals(oldPwd)){
            throw new BusinessRuntimeException(WhaleErrorCode.CURRENT_PASSWORD_ERROR);
        }
        // 更新旧的密码
        String newPwd = ShiroUtils.getEncryptPassword(kernel.getSalt(),pwdChangeReq.getPasswordNew(),kernel.getKernelName());
        kernel.setPassword(newPwd);
        userManager.updateKernel(kernel);
    }
}
